package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ffcs.crmd.tsp.admin.entity.JobGroup;
import com.ffcs.crmd.tsp.admin.service.IJobGroupService;
import com.ffcs.crmd.tsp.api.dto.client.TspJobGroup;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.core.util.BeanUtils;

/**
 * 
 * 功能说明:任务组服务
 *
 * @author FFCS-ZHONGFUHUA
 * 
 * @since 
 *
 */
@Service
public class JobGroupServiceImpl extends AbstractService implements IJobGroupService {
    
    @Override
    public void deleteJobGroup(JobGroup jobGroup) throws TspException {
        try {
            TspJobGroup tspJobGroupVO = new TspJobGroup();
            tspJobGroupVO.setClusterName(jobGroup.getClusterName());
            tspJobGroupVO.setId(jobGroup.getId());
            
            TspClient tspClient = this.getClient(jobGroup.getClusterName());
            TspResult<?> tspResult = tspClient.deleteJobGroup(tspJobGroupVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void deleteJobGroup(List<JobGroup> jobGroupList) throws TspException {
        for (JobGroup jobGroup : jobGroupList) {
            this.deleteJobGroup(jobGroup);
        }
    }
    
    @Override
    public JobGroup get(JobGroup jobGroup) throws TspException {
        try {
            TspJobGroup tspJobGroupVO = new TspJobGroup();
            BeanUtils.applyIf(tspJobGroupVO, jobGroup, false);
            
            TspClient tspClient = this.getClient(jobGroup.getClusterName());
            TspResult<TspJobGroup> tspResult = tspClient.queryJobGroup(tspJobGroupVO);
            validateResult(tspResult);
            
            JobGroup rjobGroup = new JobGroup();
            BeanUtils.applyIf(rjobGroup, tspResult.getRetVo().getObject(), false);
            return rjobGroup;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void pause(List<JobGroup> jobGroupList) throws TspException {
        for (JobGroup jobGroup : jobGroupList) {
            try {
                TspJobGroup tspJobGroupVO = new TspJobGroup();
                BeanUtils.applyIf(tspJobGroupVO, jobGroup, false);
                
                TspClient tspClient = this.getClient(jobGroup.getClusterName());
                TspResult<?> tspResult = tspClient.pauseJobGroup(tspJobGroupVO);
                validateResult(tspResult);
            } catch (Exception e) {
                throw new TspException(e.getMessage());
            }
        }
    }
    
    @Override
    public List<JobGroup> query(JobGroup jobGroup) throws TspException {
        try {
            List<JobGroup> jobGroupVOList = new ArrayList<JobGroup>();
            TspJobGroup tspJobGroupVO = new TspJobGroup();
            BeanUtils.applyIf(tspJobGroupVO, jobGroup, false);
            
            TspClient tspClient = getClient(jobGroup.getClusterName());
            TspResult<TspJobGroup> tspResult = tspClient.queryJobGroup(tspJobGroupVO);
            validateResult(tspResult);
            List<TspJobGroup> tspjobGrouPVOList = new ArrayList<TspJobGroup>();
            tspjobGrouPVOList.addAll(tspResult.getRetVo().getDataList());
            jobGroupVOList.addAll(BeanUtils.copyList(tspjobGrouPVOList, JobGroup.class)); 
            return jobGroupVOList;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public PageInfo<JobGroup> queryPage(JobGroup jobGroup, int pageNumber, int pageSize) throws TspException {
        try {
            TspJobGroup tspJobGroupVO = new TspJobGroup();
            BeanUtils.applyIf(tspJobGroupVO, jobGroup, false);
            tspJobGroupVO.setPageNumber(pageNumber);
            tspJobGroupVO.setPageSize(pageSize);
            
            TspClient tspClient = getClient(jobGroup.getClusterName());
            TspResult<TspJobGroup> tspResult = tspClient.queryJobGroup(tspJobGroupVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspJobGroup> tspJobGroupPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<JobGroup> jobGroupPageInfo = new PageInfo<JobGroup>();
            BeanUtils.applyIf(jobGroupPageInfo, tspJobGroupPageInfo, false);
            jobGroupPageInfo.setList(BeanUtils.copyList(tspJobGroupPageInfo.getList(), JobGroup.class));
            return jobGroupPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void resume(List<JobGroup> jobGroupList) throws TspException {
        for (JobGroup jobGroup : jobGroupList) {
            try {
                TspJobGroup tspJobGroupVO = new TspJobGroup();
                BeanUtils.applyIf(tspJobGroupVO, jobGroup, false);
                
                TspClient tspClient = this.getClient(jobGroup.getClusterName());
                TspResult<?> tspResult = tspClient.resumeJobGroup(tspJobGroupVO);
                validateResult(tspResult);
            } catch (Exception e) {
                throw new TspException(e.getMessage());
            }
        }
    }
    
    
    @Override
    public void saveJobGroup(JobGroup jobGroup) throws TspException {
        try {
            TspJobGroup tspJobGroupVO = new TspJobGroup();
            BeanUtils.applyIf(tspJobGroupVO, jobGroup, false);
            
            TspClient tspClient = this.getClient(jobGroup.getClusterName());
            TspResult<?> tspResult = tspClient.createJobGroup(tspJobGroupVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void updateJobGroup(JobGroup jobGroup) throws TspException {
        try {
            TspJobGroup tspJobGroupVO = new TspJobGroup();
            BeanUtils.applyIf(tspJobGroupVO, jobGroup, false);
            
            TspClient tspClient = this.getClient(jobGroup.getClusterName());
            TspResult<?> tspResult = tspClient.updateJobGroup(tspJobGroupVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
}
